<?php

switch($command) {
	case('me'): {
		if(strlen($value)) {
			$network->writeNetwork("PRIVMSG {$channel->channel} :\001ACTION {$value}\001",false);
			$channel->terminal->append(sprintf(
				'%s * %s %s',
				lcars::date(),
				$network->whoami,
				$value				
			));
		}
		break;
	}

	case('nick'): {
		if(strlen($value)) {
			$network->writeNetwork("NICK {$value}");
		}
		break;
	}

	case('shell'): {
		if(strlen($value)) {
			//. this command needs to be made better, so it can handle multiple lines.
			self::runCommand("msg {$channel->channel} ".shell_exec($value));
		}
		break;
	}

	case('quit'): {
		$network->disconnect();
		if($network->reconTimer) { Gtk::timeout_remove($network->reconTimer); }
		
		lcars::$w->text2->view->set_buffer(lcars::$w->text2->defaultbuffer);
		
		foreach($network->channel as $chanid => $chan) {
			if($chan->channel == 'null') { continue; }

			$chan->destroy();
			
			lcars::$w->nickBox["{$network->network}-{$chan->channel}"]->destroy();
			$chan->button->destroy();
			unset($chan->terminal);
		}

		$network->button->destroy();		
		lcars::$w->networkBox[$network->network]->destroy();
		lcars::$w->channelBox[$network->network]->destroy();
		break;
	}
	
	case('join'): {
		if(!array_key_exists(strtolower($value),$network->channel)) {
			if(substr($value,0,1) != '#') { $value = "#{$value}"; }
			lcars::mksnd(lcars::SND_CHAN_OPEN);
			$network->channel[strtolower($value)] = new ircChannel($value,$network);
		}
		break;
	}

	case('part'): {
		if(!$value) {
			if(lcars::$w->currentChannel->channel == 'null') {
				self::runCommand('quit');
				break;
			} else {
				if(is_object(lcars::$w->currentChannel) && lcars::$w->currentChannel->channel != 'null') {
					$value = lcars::$w->currentChannel->channel;
				}
			}
		}
		if($value && array_key_exists(strtolower($value),$network->channel)) {
			lcars::mksnd(lcars::SND_CHAN_CLOSE);
			$network->channel[strtolower($value)]->destroy();
			unset($network->channel[strtolower($value)]);

			reset($network->channel);
			$now = current($network->channel);	
			lcars::$w->setActiveChannel($now);
			
			if(substr($value,0,1) == '#') {
				$network->writeNetwork("PART {$value}");
			}
		}
		break;
	}
	
	case('open'): {
		if(!array_key_exists($value,commnet::$network)) {
			$info = explode(' ',$value);
			$name = $server = $port = null;
			if(count($info) == 3) { $name = $info[0]; $server = $info[1]; $port = $info[2]; }
			else if(count($info) == 2) { $server = $info[0]; $port = $info[1]; }
			else if(count($info) == 1) { $server = $info[0]; $port = 6667; }
			if(!$name) { $name = $server; }
			
			commnet::$network[$name] = new ircNetwork(
				$name, array(
					'server' => $server,
					'port' => $port,
					'autoconnect' => false,
					'nick' => $network->nick,
					'altnick' => $network->altnick,
					'user' => $network->user,
					'info' => $network->info,
					'channels' => array()				
				)			
			);
			
			commnet::$network[$name]->create();
			commnet::$network[$name]->connect();
		} else {
			if(commnet::$network[$value]->state & ircNetwork::CONNECTED) {
			
			} else {
				commnet::$network[$value]->create();
				commnet::$network[$value]->connect();
			}
		}
		
		break;
	}
	
	case('msg'):	 {
		$chan = substr($value,0,($msgpos = strpos($value,chr(32))));
		$msg = substr($value,($msgpos+1),(strlen($value)-$msgpos));
		
		lcars::$w->currentChannel->network->writeNetwork(sprintf(
			"PRIVMSG %s :%s",
			$chan,
			$msg
		),false);
		
		lcars::$w->currentChannel->terminal->append(sprintf(
			'%s %s: %s',
			date('YmdHis'),			
			lcars::$w->currentChannel->network->whoami,
			$msg	
		));

		break;
	}
	
	default:{
		lcars::$w->text1->buffer->append("Unknown command: {$command}");
	}
}

?>